<?php

class ResourceDB {

    public static function getResourceByID($resource_id) {
        try {
            $query = "SELECT * FROM resource WHERE resource_id = :resource_id";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->bindParam(':resource_id', $resource_id);
            $stmt->execute();
            $rowsAffect = $stmt->rowCount();
            if ($rowsAffect > 0) {
                $result = $stmt->fetch(PDO::FETCH_OBJ);
                //$result contains aynonomous object and is not compitable with any other class.
                //getCarFromPDO grabs data from the anynomous object  and returns a car object.
                return self::getResourceFromPDO($result);
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    private static function getResourceFromPDO($result) {//works
        $resource = new Resource();
        $resource->setResource_hour_rent_price($result->resource_hour_rent_price);
        $resource->setResource_id($result->resource_id);
        $resource->setResource_name($result->resource_name);
        $resource->setResource_note($result->resource_note);
        $resource->setResource_type_id($result->resource_type_id);
        $resource->setResource_identifier($result->resource_identifier);
        return $resource;
    }

    public static function getAllResources() {//works
        try {
            $query = "SELECT * FROM resource";
            $inventory = new Inventory(); //car inventory
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);

            $stmt->execute();
            $rowsAffected = $stmt->rowCount();
            if ($rowsAffected > 0) {
                $result = $stmt->fetchAll(PDO::FETCH_OBJ);

                foreach ($result as $item) {
                    $resource = self::getResourceFromPDO($item);
                    $inventory->attach($resource, $resource->getResource_id());
                }
            }
            
            if ($inventory->count() > 0) {
                $inventory->rewind();
                return $inventory;
            } else {
                return null;
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }
    public static function getAllResourcesByType($resource_type_id) {//works
        try {
            $query = "SELECT * FROM resource WHERE resource_type_id = :resource_type_id";
            $inventory = new Inventory(); //car inventory
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            
            $stmt->bindParam(':resource_type_id', $resource_type_id);
            
            $stmt->execute();
            $rowsAffected = $stmt->rowCount();
            if ($rowsAffected > 0) {
                $result = $stmt->fetchAll(PDO::FETCH_OBJ);

                foreach ($result as $item) {
                    $resource = self::getResourceFromPDO($item);
                    $inventory->attach($resource, $resource->getResource_id());
                }
            }
            
            if ($inventory->count() > 0) {
                $inventory->rewind();
                return $inventory;
            } else {
                return null;
            }
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    public static function insertResource(Resource $resource) {
        try {
            $query = "INSERT INTO resource SET 
                resource_name = :resource_name,
                resource_type_id = :resource_type_id,
                resource_identifier = :resource_identifier,
                resource_hour_rent_price = :resource_hour_rent_price,
                resource_note = :resource_note";
                
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            
            $resource_name = $resource->getResource_name();
            $resource_type_id = $resource->getResource_type_id();
            $resource_identifier = $resource->getResource_identifier();
            $resource_hour_rent_price = $resource->getResource_hour_rent_price();
            $resource_note = $resource->getResource_note();
            
            $stmt->bindParam(':resource_name', $resource_name);
            $stmt->bindParam(':resource_type_id', $resource_type_id);
            $stmt->bindParam(':resource_identifier', $resource_identifier);
            $stmt->bindParam(':resource_hour_rent_price', $resource_hour_rent_price);
            $stmt->bindParam(':resource_note', $resource_note);
            
            $stmt->execute();
            $rowsAffected = $stmt->rowCount();
            return $rowsAffected;
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    
     public static function updateResource(Resource $resource) {
        try {
            $query = "UPDATE resource SET
                resource_name = :resource_name,
                resource_type_id = :resource_type_id,
                resource_identifier = :resource_identifier,
                resource_hour_rent_price = :resource_hour_rent_price,
                resource_note = :resource_note 
                WHERE resource_id = :resource_id";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);

            $resource_name = $resource->getResource_name();
            $resource_type_id = $resource->getResource_type_id();
            $resource_identifier = $resource->getResource_identifier();
            $resource_hour_rent_price = $resource->getResource_hour_rent_price();
            $resource_note = $resource->getResource_note();
            $resource_id = $resource->getResource_id();
            
            
            $stmt->bindParam(':resource_name', $resource_name);
            $stmt->bindParam(':resource_type_id', $resource_type_id);
            $stmt->bindParam(':resource_identifier', $resource_identifier);
            $stmt->bindParam(':resource_hour_rent_price', $resource_hour_rent_price);
            $stmt->bindParam(':resource_note', $resource_note);
            $stmt->bindParam(':resource_id', $resource_id);
            
            $stmt->execute();
            $rowsAffected = $stmt->rowCount();
            return $rowsAffected;
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }


      public static function deleteResource($resource_id) {//works
        try {
            $query = "DELETE FROM resource WHERE resource_id = :resource_id";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->bindParam(':resource_id', $resource_id);
            $stmt->execute();
            $rowsAffected = $stmt->rowCount();
            return $rowsAffected;
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

    public static function getResourceCount($resource_type_id) {
        $query = "Select COUNT(*) as count From resource WHERE resource_type_id = :resource_type_id";
        $dbh = DatabaseConnection::getDB();
        $stmt = $dbh->prepare($query);
        
        $resource_type_id = $resource_type_id;
        $stmt->bindParam(':resource_type_id', $resource_type_id);
            
        $stmt->execute();
        /* Fetch the first column from the next row in the result set */
        //print("Fetch the first column from the next row in the result set:\n");
        $count = $stmt->fetchColumn();
        return $count;
    }
    
     public static function getAllResourceIDs($resource_type_id) {//works
        try {
            $query = "SELECT resource_id FROM resource WHERE resource_type_id = :resource_type_id";
            $dbh = DatabaseConnection::getDB();
            $stmt = $dbh->prepare($query);
            $stmt->bindParam(':resource_type_id', $resource_type_id);
            $stmt->execute();
            $resources_ids = $stmt->fetchAll();
            return $resources_ids;
        } catch (PDOException $e) {
            echo "Error: " . $e->getMessage();
        }
    }

}

?>
